package god530.com.simpleapp_slide.account_new.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flannery.keyboradlibrary.Keyboard;

import god530.com.simpleapp_slide.R;

public class TallyFragment extends Fragment implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change TYPES of parameters
    private String mParam1;
    private String mParam2;

    private Keyboard tally_keyboard;
    private TextView display_tv_item;
    private TextView display_tv_type;
    private TextView display_tv_account;

    private TextView tally_subs_outcome;
    private TextView tally_subs_income;
    private TextView tally_type_dress;
    private TextView tally_type_food;
    private TextView tally_type_sleep;
    private TextView tally_type_walk;
    private TextView tally_type_play;

    private String subs;
    private String types;

    public TallyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TallyFragment.
     */
    // TODO: Rename and change TYPES and number of parameters
    public static TallyFragment newInstance(String param1, String param2) {
        TallyFragment fragment = new TallyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tally, container, false);
        initView(view);
        initDisplayView(view);
        initKeyboardView(view);
        return view;
    }

    private void initView(View view) {
        tally_subs_outcome = (TextView) view.findViewById(R.id.tally_subs_outcome);
        tally_subs_income = (TextView) view.findViewById(R.id.tally_subs_income);

        tally_type_dress = (TextView) view.findViewById(R.id.tally_type_dress);
        tally_type_food = (TextView) view.findViewById(R.id.tally_type_food);
        tally_type_sleep = (TextView) view.findViewById(R.id.tally_type_sleep);
        tally_type_walk = (TextView) view.findViewById(R.id.tally_type_walk);
        tally_type_play = (TextView) view.findViewById(R.id.tally_type_play);

        tally_subs_income.setOnClickListener(this);
        tally_subs_outcome.setOnClickListener(this);

        tally_type_dress.setOnClickListener(this);
        tally_type_food.setOnClickListener(this);
        tally_type_sleep.setOnClickListener(this);
        tally_type_walk.setOnClickListener(this);
        tally_type_play.setOnClickListener(this);
    }

    private void initDisplayView(View view) {
        display_tv_item = (TextView) view.findViewById(R.id.display_tv_item);
        display_tv_type = (TextView) view.findViewById(R.id.display_tv_type);
        display_tv_account = (TextView) view.findViewById(R.id.display_tv_account);

        subs = getString(R.string.tally_type_outcome);
        types = getString(R.string.tally_outcome_dress);

        displaySubs(subs);
        displayType(types);
    }

    private void setAccoutTextView(int num) {
        String account = display_tv_account.getText().toString();
        if (!TextUtils.isEmpty(account) && account.length() < 10 && (num > -1 && num < 10)) {
            long lAccount = 0;
            try {
                lAccount = Long.parseLong(account) * 10 + num;
            } catch (NumberFormatException e) {
            }

            display_tv_account.setText(String.valueOf(lAccount));
        }
    }

    private void setCancelButton() {
        String account = display_tv_account.getText().toString();
        if (!TextUtils.isEmpty(account)) {
            long lAccount = 0;
            try {
                lAccount = Long.parseLong(account) / 10;
            } catch (NumberFormatException e) {
            }
            display_tv_account.setText(String.valueOf(lAccount));
        }
    }

    /**
     * 类别
     */
    private void displaySubs(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            display_tv_item.setText(msg);
        }
    }

    /**
     * 种类
     */
    private void displayType(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            display_tv_type.setText(msg);
        }
    }

    /**
     * 显示具体的钱数
     *
     * @param view
     */
    private void initKeyboardView(View view) {
        tally_keyboard = (Keyboard) view.findViewById(R.id.tally_keyboard);
        tally_keyboard.setVibrator(true);
        tally_keyboard.setOnKeyboardListener(new Keyboard.onKeyboardListener() {
            @Override
            public void onZeroPressed() {
                setAccoutTextView(0);
            }

            @Override
            public void onOnePressed() {
                setAccoutTextView(1);
            }

            @Override
            public void onTwoPressed() {
                setAccoutTextView(2);
            }

            @Override
            public void onThreePressed() {
                setAccoutTextView(3);
            }

            @Override
            public void onFourPressed() {
                setAccoutTextView(4);
            }

            @Override
            public void onFivePressed() {
                setAccoutTextView(5);
            }

            @Override
            public void onSixPressed() {
                setAccoutTextView(6);
            }

            @Override
            public void onSevenPressed() {
                setAccoutTextView(7);
            }

            @Override
            public void onEightPressed() {
                setAccoutTextView(8);
            }

            @Override
            public void onNinePressed() {
                setAccoutTextView(9);
            }

            @Override
            public void onAddPressed() {

            }

            @Override
            public void onSubPressed() {

            }

            @Override
            public void onMultiPressed() {

            }

            @Override
            public void onDiviPressed() {

            }

            @Override
            public void onClearPressed() {
                setCancelButton();
            }

            @Override
            public void onOkPressed() {
                // TODO 检查并存储
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        tally_keyboard.disposeVibrator();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tally_subs_income: // 收入
                displayType(getString(R.string.tally_type_income));
                break;
            case R.id.tally_subs_outcome: // 支出
                displayType(getString(R.string.tally_type_outcome));
                break;
            case R.id.tally_type_dress: // 衣
                displaySubs(getString(R.string.tally_outcome_dress));
                break;
            case R.id.tally_type_food: // 食
                displaySubs(getString(R.string.tally_outcome_food));
                break;
            case R.id.tally_type_sleep: // 住
                displaySubs(getString(R.string.tally_outcome_sleep));
                break;
            case R.id.tally_type_walk: // 行
                displaySubs(getString(R.string.tally_outcome_walk));
                break;
            case R.id.tally_type_play: //娱
                displaySubs(getString(R.string.tally_outcome_play));
                break;
        }
    }
}
